﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using B2B.Web;
using System.ServiceModel.DomainServices.Client;

namespace B2B
{
    public partial class MainPage : UserControl
    {
        List<StokUretici> Ureticiler = new List<StokUretici>();
        List<StokMarka> Markalar = new List<StokMarka>();
        List<StokKategori> Kategoriler = new List<StokKategori>();
        List<StokAnaGrup> AnaGruplar = new List<StokAnaGrup>();
        List<StokAltgrup> AltGruplar = new List<StokAltgrup>();
        b2bDomainContext ctx = new b2bDomainContext();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            FillCombo();

            cmbUreticiler.SelectionChanged += (ss, ee) =>
            {
                if (cmbUreticiler.SelectedItem != null)
                {
                    cmbMarkalar.ItemsSource = from m in Markalar where m.mrk_kod.StartsWith(((StokUretici)(cmbUreticiler.SelectedItem)).urt_kod + ".") select m;
                    cmbMarkalar.DisplayMemberPath = "mrk_ismi";
                }
            };

            cmbKategori.SelectionChanged += (ss, ee) =>
            {
                if (cmbKategori.SelectedItem != null)
                {
                    cmbAnaGrup.ItemsSource = from a in AnaGruplar where a.san_kod.StartsWith(((StokKategori)(cmbKategori.SelectedItem)).ktg_kod + ".") select a;
                    cmbAnaGrup.DisplayMemberPath = "san_isim";
                    cmbAltGrup.ItemsSource = null;
                }
            };

            cmbAnaGrup.SelectionChanged += (ss, ee) =>
            {
                if (cmbAnaGrup.SelectedItem != null)
                {
                    cmbAltGrup.ItemsSource = from a in AltGruplar where a.sta_ana_grup_kod == ((StokAnaGrup)(cmbAnaGrup.SelectedItem)).san_kod select a;
                    cmbAltGrup.DisplayMemberPath = "sta_isim";
                }
            };
        }

        private void FillCombo()
        {
            cmbUreticiler.ItemsSource = ctx.StokUreticis;
            cmbUreticiler.DisplayMemberPath = "urt_ismi";
            ctx.Load(ctx.GetStokUreticileriQuery()).Completed += (s, e) =>
                {
                    Ureticiler = ctx.StokUreticis.ToList();
                };


            cmbKategori.ItemsSource = ctx.StokKategoris;
            cmbKategori.DisplayMemberPath = "ktg_isim";
            ctx.Load(ctx.GetStokKategorileriQuery()).Completed += (s, e) =>
                {
                    Kategoriler = ctx.StokKategoris.ToList();
                };

            cmbMarkalar.ItemsSource = ctx.StokMarkas; ;
            cmbMarkalar.DisplayMemberPath = "mrk_ismi";
            ctx.Load(ctx.GetStokMarkalariQuery()).Completed += (s, e) =>
            {
                Markalar = ctx.StokMarkas.ToList();
            };

            cmbAnaGrup.ItemsSource = ctx.StokAnaGrups;
            cmbAnaGrup.DisplayMemberPath = "san_isim";
            ctx.Load(ctx.GetStokAnaGruplariQuery()).Completed += (s, e) =>
            {
                AnaGruplar = ctx.StokAnaGrups.ToList();
            };

            cmbAltGrup.ItemsSource = ctx.StokAltgrups;
            cmbAltGrup.DisplayMemberPath = "sta_isim";
            ctx.Load(ctx.GetStokAltgruplariQuery()).Completed += (s, e) =>
            {
                AltGruplar = ctx.StokAltgrups.ToList();
            };

        }


        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            //foreach (UIElement child in LinksStackPanel.Children)
            //{
            //    HyperlinkButton hb = child as HyperlinkButton;
            //    if (hb != null && hb.NavigateUri != null)
            //    {
            //        if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
            //        {
            //            VisualStateManager.GoToState(hb, "ActiveLink", true);
            //        }
            //        else
            //        {
            //            VisualStateManager.GoToState(hb, "InactiveLink", true);
            //        }
            //    }
            //}
        }

        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

    }
}